Back to course home
0% completed
Vote For New Content
Pattern 5: Longest Common Substring
Longest Common Substring
Solution: Longest Common Substring
Longest Common Subsequence
Solution: Longest Common Subsequence
Minimum Deletions & Insertions to Transform a String into another
Solution: Minimum Deletions & Insertions to Transform a String into another
Longest Increasing Subsequence
Solution: Longest Increasing Subsequence
Maximum Sum Increasing Subsequence
Solution: Maximum Sum Increasing Subsequence
Shortest Common Super-sequence
Solution: Shortest Common Super-sequence
Minimum Deletions to Make a Sequence Sorted
Solution: Minimum Deletions to Make a Sequence Sorted
Longest Repeating Subsequence
Solution: Longest Repeating Subsequence
Subsequence Pattern Matching
Solution: Subsequence Pattern Matching
Longest Bitonic Subsequence
Solution: Longest Bitonic Subsequence
Longest Alternating Subsequence
Solution: Longest Alternating Subsequence
Edit Distance
Solution: Edit Distance
Strings Interleaving
Solution: Strings Interleaving
Appendix
Contact Us
Solution: Count of Palindromic Substrings
Problem Statement
Given a string, find the total number of palindromic substrings in it. Please note we need to find the total number of substrings and not subsequences.
Example 1:
Input: "abdbca"
Output: 7
Explanation: Here are the palindromic substrings, "a", "b", "d", "b", "c", "a", "bdb".
Example 2:
Input: = "cddpd"
Output: 7
Explanation: Here are the palindromic substrings, "c", "d", "d", "p", "d", "dd", "dpd".
Example 3:
Input: = "pqr"
Output: 3
Explanation: Here are the palindromic substrings,"p", "q", "r".
Constraints:
1 <= st.length <= 1000stconsists only of lowercase English letters.
Solution
This problem follows the "Longest Palindromic Subsequence" pattern, and can be easily converted to "Longest Palindromic Substring". The only difference is that instead of calculating the longest palindromic substring, we will instead count all the palindromic substrings.
Let's jump directly to the bottom-up dynamic programming solution:
Java
Saved to Cloud
. . . .
The time and space complexity of the above algorithm is
Count of Palindromic Substrings
Minimum Deletions in a String to make it a Palindrome
Mark as Completed
On this page
Problem Statement
Solution